API Docs for: 0.0.1
Show:

File: src/event kopie/event-emitter.js

  1. "use strict";
  2.  
  3. /**
  4. * Extends the Event-instance by adding the method `Emitter` to it.
  5. * The `Emitter-method` returns an object that should be merged into any Class-instance or object you
  6. * want to extend with the emit-methods, so the appropriate methods can be invoked on the instance.
  7. *
  8. *
  9. * <i>Copyright (c) 2014 ITSA - https://github.com/itsa</i>
  10. * New BSD License - http://choosealicense.com/licenses/bsd-3-clause/
  11. *
  12. *
  13. * Should be called using the provided `extend`-method like this:
  14. * @example
  15. * var Event = require('event');<br>
  16. *
  17. * @module event
  18. * @submodule event-emitter
  19. * @class Event.Emitter
  20. * @since 0.0.1
  21. */
  22.  
  23. var NAME = '[event-emitter]: ',
  24. REGEXP_EMITTER = /^(\w|-|#)+$/,
  25. Event = require('./event-base.js');
  26.  
  27. Event.Emitter = function(emitterName) {
  28. var composeCustomevent = function(eventName) {
  29. return emitterName+':'+eventName;
  30. },
  31. newEmitter;
  32. if (!REGEXP_EMITTER.test(emitterName)) {
  33. console.error(NAME, 'Emitter invoked with invalid argument: you must specify a valid emitterName');
  34. return;
  35. }
  36. newEmitter = {
  37. /**
  38. * Defines a CustomEvent. If the eventtype already exists, it will not be overridden,
  39. * unless you force to assign with `.forceAssign()`
  40. *
  41. * The returned object comes with 4 methods which can be invoked chainable:
  42. *
  43. * <ul>
  44. * <li>defaultFn() --> the default-function of the event</li>
  45. * <li>preventedFn() --> the function that should be invoked when the event is defaultPrevented</li>
  46. * <li>forceAssign() --> overrides any previous definition</li>
  47. * <li>unHaltable() --> makes the customEvent cannot be halted</li>
  48. * <li>unPreventable() --> makes the customEvent's defaultFn cannot be prevented</li>
  49. * <li>unSilencable() --> makes that emitters cannot make this event to perform silently (using e.silent)</li>
  50. * </ul>
  51. *
  52. * @method defineEvent
  53. * @param eventName {String} name of the customEvent, without `emitterName`.
  54. * The final event that will be created has the syntax: `emitterName:eventName`,
  55. * where `emitterName:` is automaticly prepended.
  56. * @return {Object} with extra methods that can be chained:
  57. * <ul>
  58. * <li>unPreventable() --> makes the customEvent's defaultFn cannot be prevented</li>
  59. * <li>forceAssign() --> overrides any previous definition</li>
  60. * <li>defaultFn() --> the default-function of the event</li>
  61. * <li>preventedFn() --> the function that should be invoked when the event is defaultPrevented</li>
  62. * </ul>
  63. * @since 0.0.1
  64. */
  65. defineEvent: function (eventName) {
  66. return Event.defineEvent(composeCustomevent(eventName));
  67. },
  68.  
  69. /**
  70. * Emits the event `eventName` on behalf of the instance holding this method.
  71. *
  72. * @method emit
  73. * @param eventName {String} name of the event to be sent (available as e.type)
  74. * you could pass a customEvent here 'emitterName:eventName', which would
  75. * overrule the `instance-emitterName`
  76. * @param payload {Object} extra payload to be added to the event-object
  77. * @return {Promise}
  78. * <ul>
  79. * <li>on success: returnValue {Any} of the defaultFn</li>
  80. * <li>on error: reason {Any} Either: description 'event was halted', 'event was defaultPrevented' or the returnvalue of the preventedFn</li>
  81. * </ul>
  82. * @since 0.0.1
  83. */
  84. emit: function(eventName, payload) {
  85. return Event.emit(this, eventName, payload);
  86. },
  87.  
  88. /**
  89. * Removes all event-definitions of the instance holding this method.
  90. *
  91. * @method undefAllEvents
  92. * @since 0.0.1
  93. */
  94. undefAllEvents: function () {
  95. Event.undefEvent(emitterName);
  96. },
  97.  
  98. /**
  99. * Removes the event-definition of the specified customEvent.
  100. *
  101. * @method undefEvent
  102. * @param eventName {String} name of the customEvent, without `emitterName`.
  103. * The calculated customEvent which will be undefined, will have the syntax: `emitterName:eventName`.
  104. * where `emitterName:` is automaticly prepended.
  105. * @since 0.0.1
  106. */
  107. undefEvent: function (eventName) {
  108. Event.undefEvent(composeCustomevent(eventName));
  109. }
  110.  
  111. };
  112. // register the emittername:
  113. Event.defineEmitter(newEmitter, emitterName);
  114. return newEmitter;
  115. };
  116.  
  117. module.exports = Event;